<?php  

class cache{
  
    private $host = '127.0.0.1';
    private $port = 6379;
    private $pconnect = false;
    private $cacheTime = 3600;
    private $suffix = "rdsdb:";
  
    public function __construct($config){   
        if( is_array( $config ) ){   
            foreach( $config as $key=>$val ){  
                $this->$key = $val;   
            }
        }
        $this->rds = new Redis();
        if(isset($this->pconnect) && $this->pconnect == true){
            $this->rds->pconnect($this->host, $this->port);
        }else{
            $this->rds->connect($this->host, $this->port, 2);
        }
    }

    //String 缓存
    public function set($key,$val='',$leftTime=null){
        $leftTime = $leftTime ? $leftTime : $this->cacheTime;  
        return $this->rds->setex($this->suffix.$key, $leftTime, serialize($val));
    }   
    public function get($key){   
        $res = $this->rds->get($this->suffix.$key);
        return unserialize($res); 
    }
    public function mget($keys=array()){
        foreach($keys as $k=>$v) $rs_key[] = $this->suffix.$v;
        $res = $this->rds->mGet($rs_key);
        return $res;
    }

    //hash 缓存
    public function hset($key, $field, $val=''){
        return $this->rds->hset($this->suffix.$key, $field, serialize($val));
    }
    public function hget($key, $field){   
        $res = $this->rds->hget($this->suffix.$key, $field);
        return unserialize($res); 
    }
    public function hmget($key, $fields=array()){   
        $res = $this->rds->hmget($this->suffix.$key, $fields);
        return $res; 
    }
    public function hdel($key,$field){   
        return $this->rds->hdel($this->suffix.$key, $field);
    }
    public function hgetall($key){   
        return $this->rds->hgetall($this->suffix.$key);
    }
    public function hlen($key){   
        return $this->rds->hlen($this->suffix.$key);
    }

    //list
    public function rpush($key, $val=''){
        return $this->rds->rpush($this->suffix.$key, serialize($val));
    }
    public function lpop($key){
        return $this->rds->lpop($this->suffix.$key);
    }
    public function llen($key){
        return $this->rds->llen($this->suffix.$key);
    }

    //sorted set 缓存
    public function zadd($key, $score, $field){
        return $this->rds->zadd($this->suffix.$key, $score, $field);
    }
    public function zrange($key, $start, $stop){ //小到大排序
        return $this->rds->zrange($this->suffix.$key, $start, $stop);
    }
    public function zrevrange($key, $start, $stop){ //大到小排序
        return $this->rds->zrevrange($this->suffix.$key, $start, $stop);
    }
    public function zrem($key, $field){
        return $this->rds->zrem($this->suffix.$key, $field);
    }
    public function zcard($key){
        return $this->rds->zcard($this->suffix.$key);
    }

    //删除缓存
    public function del($key){   
        return $this->rds->del($this->suffix.$key);
    }
    //清除所有缓存文件
    public function clear_all(){  
        //return $this->rds->flushDb();
        $res = $this->rds->keys($this->suffix.'*');
        foreach($res as $k=>$v)
            $this->rds->del($v);
        return true;
    }
    private function error($msg){  
        die($msg);   
    }   
}   


/*
$cacheFile = new cache(array('cacheTime'=>60,'suffix'=>'.php'));   
  
$value1 = '缓存成功1';  
$value2 = '缓存成功2';  
$value3 = '缓存成功3';  
  
$cacheFile->set('key1',$value1);   
$cacheFile->set('key2',$value2);  
$cacheFile->set('key3',$value3);  
*/
  
